<?php
if (!defined('FosfoaSystem')) {exit(1);}
import($GLOBALS['spConfig']['controller_path'] . '/general.php');
class link extends general
{
    function __construct()
    {
        parent::__construct();
        $this->tpl_title = T("Dashboard");
    }
    function index()
    {
        $this->tt = "链接管理";
        $this->typearr = T('Linktype');
        $type = $this->spArgs("type",'no');
        $current_page = $this->spArgs("page", 1);
        $page_size = $this->spArgs("size", 30);

        $obj = spClass("linkModel");
        if ($type == 'no') {
            $conditions = array('1' => 1);
        } else {
            $conditions = array('link_type' => $type);
        }

        $arr = $obj->spPager($current_page, $page_size)->findAll($conditions,
            'sort desc,link_id desc');
        $arrBar = $obj->spPager()->getPager();
        $this->tPageBar = $arrBar;
        $this->arr = $arr;
        $this->type = $type;
        $this->display("admin/link_index.html");
    }
    function add()
    {
        $type = $this->spArgs("type", 0);

        $this->action = 'add';
        $this->type = $type;
        $this->addtitle = "添加链接";
        $this->typearr = T('Linktype');
        $this->display("admin/link_add.html");
    }
    function edit()
    {
        $this->typearr = T('Linktype');
        $link_id = $this->spArgs("link_id", 0);
        $Obj = spClass('linkModel');
        $conditions = array("link_id" => $link_id);
        $link = $Obj->find($conditions);
        $allimg = unserialize($link['link_imageall']);
        $i = count($allimg) - 1;
        foreach ($allimg as $key => $value) {
            //echo $key;
            if ($key == $i) {
                $img .= $value;
            } else {
                $img .= $value . '|';
            }


        }
        $this->img = $img;
        $this->allimg = $allimg;
        $this->link = $link;
        $this->type = $link['link_type'];
        $this->addtitle = "修改链接";
        $this->display("admin/link_add.html");
    }
    function save()
    {
        $strAction = $this->spArgs("action");
        $allimg = $this->spArgs("allimg");
        $allimg = explode('|', $allimg);
        $allimg = array_unique($allimg);
        $allimg = serialize($allimg);

        $data = array(
            "link_type" => $this->spArgs("type"),
            "link_title" => $this->spArgs("title"),
            "link_content" => $this->spArgs("content"),
            "link_url" => $this->spArgs('link_url'),
            'sort' => $this->spArgs('sort', 0),
            "link_imageall" => $allimg,
            "link_image" => $this->spArgs("uploadList"));

        $Obj = spClass('linkModel');
        if ($strAction == 'add') {
            $Obj->create($data);
            $msg = "链接" . $data[link_title] . "添加成功";
        } else {
            $link_id = $this->spArgs("link_id");
            $condition = array('link_id' => $link_id);
            $Obj->update($condition, $data);
            $msg = "链接" . $data[link_title] . "修改成功";
        }
        Savelog($msg);
        $result = array(
            'status' => 'y',
            'info' => $msg,
            );
        echo json_encode($result);
    }
    function del()
    {
        $Obj = spClass("linkModel");
        $type = $this->spArgs("type", 0);
        $link_id2 = $this->spArgs("link_id", 0);
        if ($link_id2 == 0) {
            $ids = $this->spArgs("selids");
            $idsarray = explode(',', $ids);
            foreach ($idsarray as $id) {
                $conditions = array('link_id' => $id);
                $Obj->delete($conditions); // 删除记录
            }
        } else {
            $conditions = array('link_id' => $link_id2);
            $Obj->delete($conditions); // 删除记录
        }
        $msg = "删除链接" . $link_id2;
        Savelog($msg);
        $this->success(T('Successfully  deleted!'), spUrl("link", "index", array("type" =>
                "$type")));
    }
}